package com.dhcc.core.framework.log.factory;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.TimerTask;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dhcc.core.framework.constant.state.LogSucceed;
import com.dhcc.core.framework.constant.state.LogType;
import com.dhcc.core.framework.log.LogManager;
import com.dhcc.core.framework.util.SpringContextHolder;
import com.dhcc.core.modules.system.dao.LoginLogMapper;
import com.dhcc.core.modules.system.dao.OperationLogMapper;
import com.dhcc.core.modules.system.entity.LoginLog;
import com.dhcc.core.modules.system.entity.OperationLog;
import com.dhcc.core.modules.system.entity.UserOnline;

/**
 * 日志操作任务创建工厂
 * 
 * @ClassName: LogTaskFactory
 * @Description: TODO
 * @author: cyf
 * @date: 2018年1月2日 上午9:55:37
 */
public class LogTaskFactory {

    private static Logger logger = LoggerFactory.getLogger(LogManager.class);

    public static TimerTask backendLoginLog(final UserOnline userOnline) {
        return loginLog(LogType.BACKEND_LOGIN,userOnline);
    }
    public static TimerTask frontLoginLog(final UserOnline userOnline) {
        return loginLog(LogType.FRONT_LOGIN,userOnline);
    }
    public static TimerTask backendLogoutLog(final UserOnline userOnline) {
        return logoutLog(LogType.BACKEND_LOGOUT,userOnline);
    }
    public static TimerTask frontLogoutLog(final UserOnline userOnline) {
        return logoutLog(LogType.FRONT_LOGOUT,userOnline);
    }
    public static TimerTask backendLoginFailLog(final UserOnline userOnline,final String username, final String msg) {
        return loginLog(LogType.BACKEND_LOGIN_FAIL,userOnline,username,msg);
    }
    public static TimerTask frontLoginFailLog(final UserOnline userOnline,final String username, final String msg) {
        return loginLog(LogType.FRONT_LOGIN_FAIL,userOnline,username,msg);
    }

    public static TimerTask loginLog(final LogType logType,final UserOnline userOnline) {
        return new TimerTask() {
            @Override
            public void run() {
                try {
                    LoginLog loginLog = LogFactory.createLoginLog(logType, userOnline, null);
                    LoginLogMapper loginLogMapper = SpringContextHolder.getBean(LoginLogMapper.class);
                    loginLogMapper.insert(loginLog);
                } catch (Exception e) {
                    logger.error("创建登录日志异常!", e);
                }
            }
        };
    }

    public static TimerTask loginLog(final LogType logType,final UserOnline userOnline,final String username, final String msg) {
        return new TimerTask() {
            @Override
            public void run() {
                userOnline.setLoginName(username);
                LoginLog loginLog = LogFactory.createLoginLog(logType,userOnline , "账号:" + username + "," + msg);
                try {
                    LoginLogMapper loginLogMapper = SpringContextHolder.getBean(LoginLogMapper.class);
                    loginLogMapper.insert(loginLog);
                } catch (Exception e) {
                    logger.error("创建登录失败异常!", e);
                }
            }
        };
    }

    public static TimerTask logoutLog(final LogType logType, final UserOnline userOnline) {
        return new TimerTask() {
            @Override
            public void run() {
                LoginLog loginLog = LogFactory.createLoginLog(logType, userOnline, null);
                try {
                    LoginLogMapper loginLogMapper = SpringContextHolder.getBean(LoginLogMapper.class);
                    loginLogMapper.insert(loginLog);
                } catch (Exception e) {
                    logger.error("创建退出日志异常!", e);
                }
            }
        };
    }

    public static TimerTask operationLog(final UserOnline userOnline, final String bussinessName, final String clazzName,
            final String methodName, final String msg) {
        return new TimerTask() {
            @Override
            public void run() {
                OperationLog operationLog = LogFactory.createOperationLog(LogType.BUSSINESS, userOnline, bussinessName,
                        clazzName, methodName, msg, LogSucceed.SUCCESS);
                try {
                    OperationLogMapper operationLogMapper = SpringContextHolder.getBean(OperationLogMapper.class);
                    operationLogMapper.insert(operationLog);
                } catch (Exception e) {
                    logger.error("创建业务日志异常!", e);
                }
            }
        };
    }

    public static TimerTask exceptionLog(final UserOnline userOnline, final Exception exception) {
        return new TimerTask() {
            @Override
            public void run() {
                String msg = getExceptionMsg(exception);
                OperationLog operationLog = LogFactory.createOperationLog(LogType.EXCEPTION, userOnline, "", null, null,
                        msg, LogSucceed.FAIL);
                try {
                    OperationLogMapper operationLogMapper = SpringContextHolder.getBean(OperationLogMapper.class);
                    operationLogMapper.insert(operationLog);
                } catch (Exception e) {
                    logger.error("创建异常日志异常!", e);
                }
            }
        };
    }

    /**
     * 获取异常的具体信息
     *
     * @author cyf
     * @Date 2017/3/30 9:21
     * @version 2.0
     */
    public static String getExceptionMsg(Exception e) {
        StringWriter sw = new StringWriter();
        try {
            e.printStackTrace(new PrintWriter(sw));
        } finally {
            try {
                sw.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
        return sw.getBuffer().toString().replaceAll("\\$", "T");
    }
}
